package world.criver.h50529.function;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import world.criver.h50529.data.daData;
import world.criver.h50529.module.moEvent;
import world.criver.h50529.module.moGroup;
import world.criver.h50529.module.moListen;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class funFiles {
    private ObjectMapper om = new ObjectMapper();;
    /**
     * 保存事件
     * @param event
     * @return
     */
    public boolean saveEvent(moEvent event){
        try {
            JsonNode jn = om.valueToTree(event);
            om.writeValue(new File("data/events/" + Base64.getUrlEncoder().encodeToString(event.getName().getBytes(StandardCharsets.UTF_8)) + ".json"), jn);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 保存所有事件
     */
    public void saveEvents(){
        for(moEvent mo: daData.events)saveEvent(mo);
    }

    /**
     * 读取所有事件
     */
    public void readEvents(){
        try{
            daData.events.clear();
            File f=new File("data/events");
            if(!f.exists() || f.isFile()){
                f.mkdirs();
                return;
            }
            File[] fs=f.listFiles();
            for(File a:fs){
                if(!a.getName().endsWith(".json"))continue;
                JsonNode jn=om.readTree(a);
                daData.events.add(om.treeToValue(jn, moEvent.class));
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除事件
     * @param name
     */
    public void deleteEvent(String name){
        try{
            File f=new File("data/events/"+Base64.getUrlEncoder().encodeToString(name.getBytes(StandardCharsets.UTF_8))+".json");
            if(!f.exists())return;
            f.delete();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * 保存监听
     * @param listen
     */
    public boolean saveListen(moListen listen){
        try{
            JsonNode jn=om.valueToTree(listen);
            om.writeValue(new File("data/listens/"+Base64.getUrlEncoder().encodeToString(listen.getName().getBytes(StandardCharsets.UTF_8))+".json"),jn);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 保存所有监听
     */
    public void saveListens(){
        for(moListen mo:daData.listens)saveListen(mo);
    }
    /**
     * 读取所有监听
     */
    public void readListens(){
        try{
            daData.listens.clear();
            File f=new File("data/listens");
            if(!f.exists() || f.isFile()){
                f.mkdirs();
                return;
            }
            File[] fs=f.listFiles();
            for(File a:fs){
                if(!a.getName().endsWith(".json"))continue;
                JsonNode jn=om.readTree(a);
                daData.listens.add(om.treeToValue(jn, moListen.class));
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除事件
     * @param name
     */
    public void deleteListen(String name){
        try{
            File f=new File("data/listens/"+Base64.getUrlEncoder().encodeToString(name.getBytes(StandardCharsets.UTF_8))+".json");
            if(!f.exists() || f.isDirectory())return;
            f.delete();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * 加载转发数据
     */
    public void loadTransmit(){
        try{
            daData.groups.clear();
            File f=new File("data/groups");
            if(!f.exists())f.mkdirs();
            File[] fs=f.listFiles();
            for(File ff:fs)daData.groups.add(om.treeToValue(om.readTree(ff), moGroup.class));
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * 保存转发数据
     */
    public void saveTransmit(){
        try{
            for(moGroup mo:daData.groups){
                String name=Base64.getUrlEncoder().encodeToString(mo.getName().getBytes(StandardCharsets.UTF_8));
                File f=new File("data/groups/"+name+".json");
                if(!f.exists()){
                    f.getParentFile().mkdirs();
                    f.createNewFile();
                }
                om.writeValue(f,om.valueToTree(mo));
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除转发数据
     * @param name
     */
    public void deleteTransmit(String name){
        try{
            name=Base64.getUrlEncoder().encodeToString(name.getBytes(StandardCharsets.UTF_8));
            File f=new File("data/groups/"+name+".json");
            f.delete();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
